package com.naiterui.common.repository.config;

import java.lang.reflect.AnnotatedElement;
import java.util.List;

import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;

/**
 * <pre>
 *      效果等于 @Transactional(rollbackFor=Exception.class)
 * </pre>
 *
 * @author 王泽浩
 * @date 2018/7/12 下午6:15
 * @since 1.0.0
 */
@SuppressWarnings("unused")
public class CustomAnnotationTransactionAttributeSource extends AnnotationTransactionAttributeSource {
    @Override
    protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) {
        TransactionAttribute attribute = super.determineTransactionAttribute(ae);
        if (attribute == null) {
            return null;
        }
        RuleBasedTransactionAttribute ruleBaseAttr = (RuleBasedTransactionAttribute) attribute;
        List<RollbackRuleAttribute> rollbackRules = ruleBaseAttr.getRollbackRules();
        if (rollbackRules.isEmpty()) {
            rollbackRules.add(new RollbackRuleAttribute(java.lang.Exception.class));
        }
        return attribute;
    }
}
